// pages/mendsite/mendsite.js
let network = require("../../utils/network.js")
let app = getApp();
let url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickerHidden: true,
    chosen: '',
    customItem: [],
    detailed: '请选择地址',
    error: '',
    mend:{}
  },

  switch1Change(e) {
    this.setData({
      'mend.is_default': !this.data.mend.is_default
    })
  },

  formSubmit(e) {
    var that = this
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (e.detail.value.collect == "") {
      that.data.error = '请输入收件人姓名'
      that.getUserInfo()
      return false;
    } else if (e.detail.value.phone == "") {
      that.data.error = '请输入手机号'
      that.getUserInfo()
      return false;
    } else if (e.detail.value.textarea == "") {
      that.data.error = '请输入详细地址'
      that.getUserInfo()
      return false;
    } else if (!myreg.test(e.detail.value.phone)) {
      that.data.error = '手机号有误'
      that.getUserInfo()
      return false;
    } else {
      network.request({
        url: url+'/AppletsApi/Address/addressEdit.html',
        data: {
          applet_openid: wx.getStorageSync('applet_openid'),
          name: e.detail.value.collect,
          mobile: e.detail.value.phone,
          address: e.detail.value.textarea,
          address_id: that.data.mend.address_id
        },
        method: 'post',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (data) {
          if (data.data.msg == '修改成功'){
            wx.navigateTo({
              url: '/pages/confirmorder/confirmorder?id=' + that.data.mend.address_id,
            })
          }
        }
      })
    }
  },
  //省市联动
  bindRegionChange: function (e) {
    var that = this
    //为了让选择框有个默认值，    
    that.setData({
      clas: ''
    })　　　//下拉框所选择的值
    console.log('picker发送选择改变，携带值为', e.detail.value)

    this.setData({
      //拼的字符串传后台
      detailed: e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2],
      //下拉框选中的值
      region: e.detail.value
    })

    this.setData({
      "AddSite.area": e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2]
    })
    console.log(this.data.AddSite)
  },

  getUserInfo() {
    let that = this
    wx.showToast({
      title: that.data.error,
      icon: 'none',
      duration: 1000
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    that.setData({
      mend: options
    })
    if (that.data.mend.is_default == 1) {
      that.setData({
        'mend.is_default': true
      })
    } else {
      that.setData({
        'mend.is_default': false
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})